<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2019 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2019-06-21
 * Time: 13:23
 */

namespace App\Repositories\Eloquent;


use App\Models\User;
use App\Repositories\Contracts\UserRepositoryInterface;
use Illuminate\Support\Facades\Hash;

class UserRepository extends BaseRepository implements UserRepositoryInterface
{
    /**
     * @return User|\Illuminate\Database\Eloquent\Builder
     */
    public function query()
    {
        // TODO: Implement query() method.
        return User::query();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|string
     */
    public function model()
    {
        // TODO: Implement getModel() method.
        return User::class;
    }

    /**
     * @param array $attributes
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     * @throws \Exception
     */
    public function create(array $attributes)
    {
        if (isset($attributes['password'])) {
            $attributes['password'] = Hash::make($attributes['password']);
        }else{
            unset($attributes['password']);
        }
        return parent::create($attributes); // TODO: Change the autogenerated stub
    }

    /**
     * @param array $attributes
     * @return bool|mixed
     * @throws \Exception
     */
    public function update(array $attributes)
    {
        if (isset($attributes['password'])) {
            $attributes['password'] = Hash::make($attributes['password']);
        }else{
            unset($attributes['password']);
        }
        return parent::update($attributes); // TODO: Change the autogenerated stub
    }

    /**
     * @param User $user
     * @return mixed|string
     */
    public function getAccessToken($user)
    {
        return $user->createToken(\Laravel\Passport\Client::find(1)->name)->accessToken;
    }
}
